package views;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import models.Dia_Semana;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import com.lavantech.gui.comp.DateTimePicker;
import com.lavantech.gui.comp.TimePanel;
import com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBaseIterators.ParentIterator;

import controllers.Modificar_Dia_Agenda_Controller;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Pantalla_Dia extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JTextField txtFrecuencia;
	private JButton btnCancelar;
	private JPanel jPanelFinal;
	private JPanel jPanelInicial;
	private JButton btnOk;
	private JCheckBox cBoxDiaLaboral;
	private JLabel lblFrecuencia;
	private Actions l= new Actions();
	private Configurar_Agenda parent;
	private JLabel lblHorarioDeInicio;
	private JLabel lblHorarioFinal;
	private Modificar_Dia_Agenda_Controller controller=Modificar_Dia_Agenda_Controller.getInstancia();
	private Dia_Semana diaActual;
	private TimePanel timePickerInicial;
	private TimePanel timePickerFinal;
	private Time timeInicial;
	private Time timeFinal;
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	public Pantalla_Dia(Configurar_Agenda parent) {
		super();
		initGUI();
		cambiarVisibilidadDeTodo(false);
		this.parent=parent;
		cleanScreen();
		jPanelInicial.setVisible(false);
		jPanelFinal.setVisible(false);

	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Editar Dia -");
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(691, 635));
			{
				lblHorarioFinal = new JLabel();
				getContentPane().add(lblHorarioFinal, new AnchorConstraint(209, 908, 236, 599, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				lblHorarioFinal.setText("Seleccione Horario Final");
				lblHorarioFinal.setPreferredSize(new java.awt.Dimension(211, 16));
			}
			{
				lblHorarioDeInicio = new JLabel();
				getContentPane().add(lblHorarioDeInicio, new AnchorConstraint(209, 416, 237, 103, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				lblHorarioDeInicio.setText("Seleccione horario de inicio");
				lblHorarioDeInicio.setPreferredSize(new java.awt.Dimension(214, 17));
			}
			{
				jPanelFinal = new JPanel();
				getContentPane().add(jPanelFinal, new AnchorConstraint(268, 909, 619, 599, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				jPanelFinal.setPreferredSize(new java.awt.Dimension(212, 209));
				{
					timePickerInicial= new TimePanel();
					timePickerInicial.setPreferredSize(new java.awt.Dimension(239, 198));
					timePickerInicial.getClockPanel().setPreferredSize(new java.awt.Dimension(226, 88));
					jPanelFinal.add(timePickerInicial);
				}
			}
			{
				jPanelInicial = new JPanel();
				getContentPane().add(jPanelInicial, new AnchorConstraint(268, 416, 619, 94, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				jPanelInicial.setPreferredSize(new java.awt.Dimension(220, 209));
				{
					timePickerFinal= new TimePanel();
					
					timePickerFinal.getClockPanel().setPreferredSize(new java.awt.Dimension(226, 132));
					timePickerFinal.setPreferredSize(new java.awt.Dimension(235, 198));			
					jPanelInicial.add(timePickerFinal);
					
				
					
				}
			}
			{
				lblFrecuencia = new JLabel();
				getContentPane().add(lblFrecuencia, new AnchorConstraint(755, 296, 807, 28, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				lblFrecuencia.setText("Frecuencia (hh.mm):");
				lblFrecuencia.setPreferredSize(new java.awt.Dimension(183, 31));
			}
			{
				txtFrecuencia = new JTextField();
				getContentPane().add(txtFrecuencia, new AnchorConstraint(753, 570, 802, 273, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				txtFrecuencia.setPreferredSize(new java.awt.Dimension(203, 29));
			}
			{
				cBoxDiaLaboral = new JCheckBox();
				getContentPane().add(cBoxDiaLaboral, new AnchorConstraint(49, 237, 121, 32, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				cBoxDiaLaboral.setText("Dia Laboral ?");
				cBoxDiaLaboral.setPreferredSize(new java.awt.Dimension(140, 43));
				cBoxDiaLaboral.addActionListener(l);
			}
			{
				btnOk = new JButton();
				getContentPane().add(btnOk, new AnchorConstraint(883, 803, 948, 683, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				btnOk.setText("Ok");
				btnOk.setPreferredSize(new java.awt.Dimension(82, 39));
				btnOk.addActionListener(l);
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar, new AnchorConstraint(881, 977, 948, 857, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				btnCancelar.setText("Cancelar");
				btnCancelar.setPreferredSize(new java.awt.Dimension(82, 40));
				btnCancelar.addActionListener(l);
			}
			{

				
			}
			pack();
			this.setSize(691, 635);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void cambiarVisibilidadDeTodo(boolean valor)
	{
		txtFrecuencia.setVisible(valor);
		lblFrecuencia.setVisible(valor);
		jPanelFinal.setVisible(valor);
		jPanelInicial.setVisible(valor);
		lblHorarioDeInicio.setVisible(valor);
		lblHorarioFinal.setVisible(valor);
	}
	public void cleanScreen()
	{
		txtFrecuencia.setText("");
	}
	public class Actions implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			if(source == cBoxDiaLaboral)
			{
				if(cBoxDiaLaboral.isSelected())
				{
					cambiarVisibilidadDeTodo(true);
				}
				else{
					cambiarVisibilidadDeTodo(false);
				}
			}
			else if (source == btnOk)
			{
				if (cBoxDiaLaboral.isSelected())
				{
					boolean validar;
					timeFinal=new Time(timePickerFinal.getCalendar().getTimeInMillis());
					timeInicial= new Time(timePickerInicial.getCalendar().getTimeInMillis());
					
					System.out.println("time i: "+ timeInicial);
					System.out.println("time f: "+ timeFinal);
					validar=controller.validarCampos(txtFrecuencia.getText(),timePickerInicial.getCalendar(),timePickerFinal.getCalendar());
					if (validar)
					{
						diaActual=controller.modificarDiaSemana(diaActual, true, timeFinal, timeInicial);
						JOptionPane.showMessageDialog(null, "Cambios realizados correctamente");
						parent.setEnabled (true);
						cleanScreen();
						dispose();
						parent.setTableModel();
					}
				}
				else
				{
					dispose();
					diaActual= controller.modificarDiaSemana(diaActual,false,null, null);
					parent.setEnabled(true);
				}
			}
			else if (source == btnCancelar)
			{
				parent.setEnabled(true);
				cleanScreen();
				setVisible(false);
			}
			
		}
		
	}
	public void setDiaSemana(Dia_Semana diaAt) {
		diaActual= diaAt;	
	}

}
